package version.test.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;

import java.util.Date;

/**
 * @description:token加密解密
 * @author: hzw
 * @time: 2023/12/28 17:40
 */
public class JWTUtil {

    private static long expire = 60*60;
    private static String secret = "hzwlyxhya1314ppaa";

    public static String generateToken(String userIdAndName) {
        Date expiration = new Date(System.currentTimeMillis() + expire);
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setSubject(userIdAndName)
                .setIssuedAt(new Date())
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.ES512, secret).compact();
    }

    public static String getClaimsByToken(String token) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody().getSubject();
    }
}
